package springmybatis.service.impl;

import org.springframework.transaction.annotation.Transactional;
import springmybatis.dao.GoodsDao;
import springmybatis.dao.SaleDao;
import springmybatis.domain.Goods;
import springmybatis.domain.Sale;
import springmybatis.service.BuyGoodsService;

public class BuyGoodsServiceImpl implements BuyGoodsService {

    private GoodsDao goodsDao;
    private SaleDao saleDao;

    public GoodsDao getGoodsDao() {
        return goodsDao;
    }

    public void setGoodsDao(GoodsDao goodsDao) {
        this.goodsDao = goodsDao;
    }

    public SaleDao getSaleDao() {
        return saleDao;
    }

    public void setSaleDao(SaleDao saleDao) {
        this.saleDao = saleDao;
    }

//@Transactional
    @Override
    public void buy(Integer goodsId, Integer amount) {

        Goods goods = goodsDao.selectGoods(goodsId);

        //减库存
        if (goods != null) {
            goods.setId(goodsId.toString());
            goods.setNums(amount);
            goodsDao.updateGoods(goods);
        }


        System.out.println(1/0);
        //增加销售记录
        Sale sale = new Sale();
        sale.setId("1adffdsas");
        sale.setGid(goodsId.toString());
        sale.setNums(amount);

        saleDao.insertSale(sale);


    }
}
